0. Attach packages

library(tidyverse)
library(janitor)
library(lubridate)
library(tsibble)
# library(tsibbledata)
library(feasts) # Possibly get dev version (gg_season issue): remotes::install_github("tidyverts/feasts")
library(forecast)
library(paletteer)

1. Get the data

We’ll explore, then forecast, US energy consumption and production by renewables source. Get the data from renewables_cons_prod.csv:

us_renew <- read_csv("renewables_cons_prod.csv") %>% 
  clean_names()

Explore the data frame:

# View(us_renew)
# names(us_renew)
# unique(us_renew$description)

We’ll focus on consumption data.

Clean up data

renew_clean <- us_renew %>% 
  mutate(description = str_to_lower(description)) %>% 
  filter(str_detect(description, pattern = "consumption")) %>% 
  filter(!str_detect(description, pattern = "total"))

Convert yyyymm column to tsibble with lubridate

renew_date <- renew_clean %>% 
  mutate(month = lubridate::parse_date_time(yyyymm, "ym")) %>% 
  mutate(month = yearmonth(month)) %>% #coerce to `yearmonth` format
  mutate(value = as.numeric(value)) %>% 
  drop_na(month, value)

Make a ggplot

Make, then save, the base line plot as renew_gg:

renew_gg <- ggplot(data = renew_date, aes(x = month, y = value, group = description)) +
  geom_line(aes(color = description)) +
  theme_minimal() +
  scale_y_continuous(limits = c(0, 350))

Now try updating your color palette using options from paletteer. Use View(palettes_d_names) to see all of the discrete scale options. We’ll want a palette with at least 7 colors (length >= 7). Find a name of a palette that you like, then update your graph by adding scale_color_paletteer_d("package::palette"). Like, if I want to use the calecopal::figmtn palette, I’d add:

renew_gg + scale_color_paletteer_d("calecopal::figmtn")

Try some out!

renew_gg +
  scale_color_paletteer_d("calecopal::figmtn")

Have some fun trying out different color palettes.

Coerce to a tsibble:

renew_ts <- as_tsibble(renew_date, key = description, index = month)

Look at the data in a few different ways:

renew_ts %>% autoplot(value)

renew_ts %>% gg_subseries(value)

renew_ts %>% gg_season(value)

Get just the wind energy consumption data:

hydro_ts <- renew_ts %>% 
  filter(description == "hydroelectric power consumption")

# Explore: 
hydro_ts %>% autoplot(value)

hydro_ts %>% gg_subseries(value)

hydro_ts %>% gg_season(value)

Some hydro power consumption decomposition

First, let’s check the decomposition (STL):

# Find STL decomposition
dcmp <- hydro_ts %>%
  model(STL(value ~ season(window = Inf)))

# View the components
# components(dcmp)

# Visualize the decomposed components
components(dcmp) %>% autoplot() +
  theme_minimal()